package control;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.ImageIcon;
import java.util.*;
import javax.swing.*;
public class Crop   extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 200L;
	Image image;
	ArrayList<Image> images = new ArrayList<Image>();
	Insets insets;

	public Crop(String imgs, int cropWidth, int cropHeight) {
		super();
		ImageIcon icon = new ImageIcon(this.getClass().getResource(imgs));
		image = icon.getImage();
		startCrop_1(0,0,cropWidth,cropHeight,image.getWidth(icon.getImageObserver()),image.getHeight(icon.getImageObserver()));
	}
	
	public int getInsetsX() {
		return insets.left;
	}
	public int getInsetY() {
		return insets.top;
	}
	public void startCrop_1(int x, int y, int w, int h, int imgW, int imgH) {
		if(w<=imgW && h<=imgH){
			if(x+w <= imgW) {
				if(y+h <= imgH) {
					images.add(createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x, y, w, h))));
						startCrop_1(x+w,y,w,h,imgW,imgH);
				}
			}
			else {
				x=0;
				startCrop_1(x,y+h,w,h,imgW,imgH);
			}
		}
	}
	public ArrayList<Image> getImages() {
		return images;
	}
	public void paint(Graphics g) {
		super.paint(g);
		if (insets == null) {
		  insets = getInsets();
		}
		g.drawImage(image, insets.left, insets.top, this);
	}
}